//
//  ViewController.m
//  ScreeningOfNewFont
//
//  Created by LuckyStrike on 2017/5/20.
//  Copyright © 2017年 LuckyStrike. All rights reserved.
//

#import "ViewController.h"
#import "FontCell.h"
#import "ZTFontManager.h"




@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic,strong) UITableView *myTableView;
/** 原生字体  */
@property (nonatomic,strong) NSArray *fontNames;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"NativeFamilyNames" ofType:@"txt"];
//    NSString *tempStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

#if IncludeSystemFont
    self.fontNames = [ZTFontManager getNativeFamilyNmaes];
#else
    if ([ZTFontManager checkExistForNewFont]) {
        self.fontNames = [ZTFontManager getNewFamilyNmae];
    }
#endif
    
    self.myTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.view addSubview:self.myTableView];
    
    [self.myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([FontCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
    
    
    for (NSString *name in [UIFont fontNamesForFamilyName:@"PingFang SC"]) {
//        NSLog(@"%@",name);
    }
}

- (BOOL)prefersStatusBarHidden{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.fontNames.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [UIFont fontNamesForFamilyName:self.fontNames[section]].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FontCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[FontCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSString *familyName = [self.fontNames objectAtIndex:indexPath.section];
    NSString *fontName = [[UIFont fontNamesForFamilyName:familyName] objectAtIndex:indexPath.row];
    
    cell.textLab.text = fontName;
//    NSLog(@"fontName:%@",fontName);
    cell.detailLab.text = @"这是中文字体,English Font,12356";
    cell.detailLab.font = [UIFont fontWithName:fontName size:22];
    cell.detailLab.adjustsFontSizeToFitWidth = YES;
    
    return cell;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.fontNames objectAtIndex:section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *familyName = [self.fontNames objectAtIndex:indexPath.section];
    NSString *fontName = [[UIFont fontNamesForFamilyName:familyName] objectAtIndex:indexPath.row];
    
    UIPasteboard *pb = [UIPasteboard generalPasteboard];
    pb.string = fontName;KohinoorGujarati-Bold
    
    [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}

@end
